package com.imddysc.jzknavigator.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.google.common.base.Strings;
import com.imddysc.jzknavigator.utils.PathNode;

@Service
public class ZknavigatorService {
	
	private static final Logger logger = LoggerFactory.getLogger(ZknavigatorService.class);

	@Value("${jzknavigator.zookeeper.host}")
	private String zkHost;
	
	@Value("${jzknavigator.zookeeper.port}")
	private String zkPort;
	
	private CuratorFramework curatorFramework;
	
	public void init() {
		logger.info("init ");
		String connStr = zkHost + ":" + zkPort;
		CuratorFramework cur = CuratorFrameworkFactory.builder()
				.connectString(connStr)
				.connectionTimeoutMs(10000)
				.retryPolicy(new ExponentialBackoffRetry(1000, 3))
				.build();
		cur.start();// 连接
		curatorFramework = cur;
	}
	
	public String getData(String path) throws Exception {
		String a = new String(curatorFramework.getData().forPath(path), "UTF-8");
		return a;
	}
	
	public List<String> getChildren(String path) throws Exception {
		List<String> result = new ArrayList<String>();
		logger.info("result: " + result);
		result=  curatorFramework.getChildren().forPath(path);
		logger.info("result: " + result);
		return result;
	}
	
	public void putData(String path, String data) throws Exception {
		curatorFramework.setData().forPath(path, data.getBytes("UTF-8"));
	}
	
	//public void createAndPutData(String path, String data) throws Exception {
		//curatorFramework.create().withMode(CreateMode.PERSISTENT).forPath(path, data.getBytes("UTF-8"));
	//}
	
	public void createAndPutData(String path, String data) throws Exception {
		if (StringUtils.isEmpty(data)) {
			curatorFramework.create().withMode(CreateMode.PERSISTENT).forPath(path, "".getBytes("UTF-8"));
		} else {
			curatorFramework.create().withMode(CreateMode.PERSISTENT).forPath(path, data.getBytes("UTF-8"));
		}
		
	}

	public boolean checkExists(String path) throws Exception {
		if (curatorFramework.checkExists().forPath(path) == null) {
			return false;
		} else {
			return true;
		}
	}
	
	public void deletePathNode(String path) throws Exception {
		curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath(path);
	}
	
	public List<PathNode> getPathNodesForPath(String path) throws Exception {
		List<PathNode> pathNodeList = new ArrayList<PathNode>();
		List<String> pathNodes = getChildren(path);
		logger.info("path: " + path);
		logger.info("pathNodes: " + pathNodes);
		for (String pn : pathNodes) {
			PathNode pathNode = new PathNode();
			String t;
			if ( path.equals("/")) {
				t = "/" + pn;
				logger.info("t1: " + t);
			} else {
				t = path + "/" + pn;
			}
			pathNode.setCurname(pn);
			pathNode.setPath(t);
			pathNode.setCpath(t.replace("/", "|"));
			logger.info("t: " + t);
			String v = getData(t);
			if ( !Strings.isNullOrEmpty(v)) {
				pathNode.setContent(v);
			}
			
			pathNodeList.add(pathNode);
		}
		return pathNodeList;
	}
	
	
	
	
}
